﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Centny.PdfReader.Types
{
    public class PdfString : PdfClass
    {
        public String Data { get; set; }
        public byte[] Bys { get; set; }
        public PdfString()
        {


        }
        public PdfString(String d, bool hex = false)
        {
            if (hex)
            {
                this.InitHexString(d);
            }
            else
            {
                this.Data = d;
            }
            this.Bys = Encoding.UTF8.GetBytes(this.Data);
        }
        public PdfString(byte[] d, bool hex = false)
        {
            if (hex)
            {
                int len = d.Length / 2;
                int remain = d.Length % 2;
                int idx = 0;
                MemoryStream ms = new MemoryStream();
                for (int i = 0; i < len; i++)
                {
                    String t = ((char)d[idx]) + "" + ((char)d[idx + 1]);
                    byte bys = Convert.ToByte(t, 16);
                    ms.WriteByte(bys);
                    idx += 2;
                }
                if (remain > 0)
                {
                    String t = ((char)d[idx]) + "0";
                    byte bys = Convert.ToByte(t, 16);
                    ms.WriteByte(bys);
                }
                this.Bys = ms.ToArray();
                this.Data = Encoding.UTF8.GetString(this.Bys, 0, this.Bys.Length);
            }
            else
            {
                this.Bys = d;
                this.Data = Encoding.UTF8.GetString(d, 0, d.Length);
            }
        }
        public void InitHexString(String hex)
        {
            this.Data = hex;
        }
    }
}
